package groupb6.shopsystem.controller.customer;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import groupb6.shopsystem.pojo.entity.Destination;
import groupb6.shopsystem.pojo.entity.User;
import groupb6.shopsystem.pojo.form.AddAddressForm;
import groupb6.shopsystem.pojo.form.CustomerEditForm;
import groupb6.shopsystem.pojo.form.UpdateAddressForm;
import groupb6.shopsystem.pojo.res.UserInfoRes;
import groupb6.shopsystem.util.enums.ResponseEnum;
import groupb6.shopsystem.util.response.UniversalResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import groupb6.shopsystem.service.CustomerInfoService;

import java.util.List;

@Api(tags = "13:个人资料页面")
@ApiSupport(author = "zyliang")
@RestController
@RequestMapping("/customer/address") // check
public class CustomerInfoController {
    @Autowired
    private CustomerInfoService customerInfoService;

    @ApiOperation("获取该用户所有地址")
    @GetMapping("/get") // check
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功")})
    @ResponseBody
    public UniversalResponse<List<Destination>> getDestinations() {
        return customerInfoService.getDestinations();
    }

    @ApiOperation("修改地址")
    @PostMapping("/update") // check
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1003, message = "没有指定修改的地址"),})
    @ResponseBody
    public UniversalResponse<?> updateDestination(@RequestBody UpdateAddressForm updateAddressForm) {
        return customerInfoService.updateUserDestination(updateAddressForm);
    }

    @ApiOperation("删除地址")
    @PostMapping("/delete") // check
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1003, message = "没有指定删除的地址"),})
    @ResponseBody
    public UniversalResponse<?> deleteDestination(@RequestBody Integer id) {
        return customerInfoService.deleteUserDestination(id);
    }

    @ApiOperation("新增地址")
    @PostMapping("/add") // check
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功")})
    @ResponseBody
    public UniversalResponse<?> insertDestination(@RequestBody AddAddressForm addAddressForm) {
        return customerInfoService.insertUserDestination(addAddressForm);
    }

    @ApiOperation("修改默认地址")
    @PostMapping("/changeDefault") // check
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功")})
    @ResponseBody
    public UniversalResponse<?> changeDefaultDestination(@RequestParam Integer destination_id) {
        return customerInfoService.changeDefaultDestination(destination_id);
    }

    @ApiOperation("获取个人信息")
    @GetMapping("/getinfo")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功")})
    @ResponseBody
    public UniversalResponse<UserInfoRes> info() {
        UserInfoRes res=new UserInfoRes();
        res.setUser(customerInfoService.getInfo());
        res.setDestinationList(customerInfoService.getDestinations().getData());
        return new UniversalResponse<UserInfoRes>(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getMsg(),res);
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/editinfo")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功")})
    @ResponseBody
    public UniversalResponse<?> editinfo(@RequestBody CustomerEditForm form ) {
        customerInfoService.editInfo(form);
        return new UniversalResponse<>().ok();
    }
}